﻿using HimVoiceStudioCore;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace HimVoiceStudio
{
    internal static class Program
    {
        public static string openingPath = null;

        public static string appPath = null;

        public static string cachePath = null;

        public static PreloadAssets PAssets = null;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
#if DEBUG
            // appPath = @"Q:\MachineLearning\HimAIVoiceStudio\HimVoiceStudio\"; 
            appPath = @"Q:\MachineLearning\HimVoiceStudio\";
#else
            appPath = Path.GetDirectoryName(Application.ExecutablePath);
#endif
            
            cachePath = Path.Combine(Path.GetTempPath(),"com.zyfdroid.himvoicestudio", "audiocache2");
            // if(!Directory.Exists(appPath)) Directory.CreateDirectory(appPath);
            if(!Directory.Exists(cachePath)) Directory.CreateDirectory(cachePath);
            if(args.Length != 0) {
                string pathInParam = args[0];
                pathInParam = pathInParam.Trim('\'','"');
                if(File.Exists(pathInParam)) {
                    openingPath = pathInParam;
                }
            }
            ComposingService.CachePath = cachePath; 
            PAssets = new PreloadAssets(Path.Combine(appPath, "assets", "extra"));

            ComposingService.SetAssetProvider(PAssets);
            // load syntax highlight
            IHighlightingDefinition customHighlight;
            using(MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Properties.Resources.syntax)))
            {
                using(XmlReader reader = XmlReader.Create(ms))
                {
                    customHighlight = HighlightingLoader.Load(reader,HighlightingManager.Instance);
                    HighlightingManager.Instance.RegisterHighlighting("Him voice studio transcription", new string[] { ".hvst" }, customHighlight);
                }

            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmMain());
        }
    }
}
